package cn.wolfcode.luowowo.website.exception;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;

/**
 * 统一自定义的异常的处理,使代码优雅
 */
@ControllerAdvice
public class UnitiveExceptionHandler {

    @ExceptionHandler(LogicException.class)
    public void handlerLogicException(LogicException e , HttpServletResponse response) throws Exception {
        e.printStackTrace();
        //使用response返回值得时候,记得设置contentType
        response.setContentType("test/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(new JsonResult(false,e.getMessage())));
    }

    @ExceptionHandler(RuntimeException.class)
    public void handlerRuntimeException(LogicException e , HttpServletResponse response) throws Exception {
        e.printStackTrace();
        //使用response返回值得时候,记得设置contentType
        response.setContentType("test/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(new JsonResult(false,"系统繁忙,请稍后再试")));
    }
}
